package sdu.icat.ltp.web.controller;

import java.util.List;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sdu.icat.ltp.service.ApiInService;
import sdu.icat.ltp.service.ApiOutService;
import sdu.icat.ltp.tool.DataBaseFascade.Context;
import sdu.icat.ltp.tool.TextBreaker;

@RestController
@RequestMapping("ltp")
@Api("ltp-api")
public class ApiController {

    @Autowired private ApiInService apiInService;
    
    @Autowired private ApiOutService apiOutService;
    
    @PostMapping("/input")
    @ApiOperation(value="发送请求",notes="将请求内容发送到ltp平台进行分析，并获取分析后数据,成功返回Ok")
    public String getTextStorage(@RequestParam("text") String text) throws InterruptedException {
	if(!apiInService.status()) {
	    apiInService.start();
	}
	apiInService.putEntity(text);
	return "OK";
    }
    
    @PostMapping("/tinput")
    @ApiOperation(value="发送请求",notes="将请求内容发送到ltp平台进行分析，并获取分析后数据,成功返回Ok")
    public String getTitleTextStorage(String title, String text) throws InterruptedException {
	if(!apiInService.status()) {
	    apiInService.start();
	}
	apiInService.putEntity(title+"&space"+text);
	return "OK";
    }
    
    @GetMapping("/output")
    @ApiOperation(value="获取数据",notes="获取mongodb里的所有数据")
    public List<Document> getDocuments() {
	return apiOutService.listSimpleView();
    }
    
    @GetMapping("/keyin")
    @ApiOperation(value="模糊查询",notes="根据key获取mongodb里的模糊数据")
    public List<Document> findDocuments(@RequestParam("key") String key) {
	return apiOutService.listRegexText(key);
    }
    
    @GetMapping("/keyeq")
    @ApiOperation(value="精确查询",notes="根据key获取mongodb里的精确数据")
    public Document findDocument(@RequestParam("key") String key) {
	return apiOutService.findMyText(key);
    }
    
    @GetMapping("/article")
    @ApiOperation(value="获取文档库",notes="根据标题，查找库中存储的解析数据")
    public List<Document> findDocumentWithTitle(String title) {
	if(title==null)
	    return null;
	return apiOutService.listArticle(title);
    }
    
    @GetMapping("/bigarticle")
    @ApiOperation(value="获取文档库",notes="根据标题，查找库中存储的解析数据")
    public List<Document> findArticleWithTitle(int bookid,String title,int sort) {
	if(title==null)
	    return null;
	return apiOutService.getAllArticles(bookid, title, sort);
    }
    
    @PostMapping("/split")
    @ApiOperation(value="切分句子")
    public String[] splitText(String text) {
	if(text==null) {
	    return null;
	}
	return TextBreaker.TextBreak(text);
    }
    
    @GetMapping("/all/articles")
    @ApiOperation(value="获取所有文章信息")
    public List<String> getAllArticles() {
	return apiInService.listArticle();
    }
    
    @GetMapping("/all/chapters")
    @ApiOperation(value="获取所有章节信息",notes="根据书名获取信息")
    public List<String> getAllChapters(String booktitle) {
	return apiInService.listChapter(booktitle);
    }
    
    @GetMapping("/contexts")
    @ApiOperation(value="获取所有内容")
    public List<Context> getContexts(String booktitle,String chaptertitle) {
	return apiInService.getContexts(booktitle, chaptertitle);
    }
}
